package cn.zmit.kuxi.holder;

import com.xdroid.common.utils.PreferenceHelper;
import com.xdroid.functions.holder.ViewHolderBase;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.activity.HisCenterActivity;
import cn.zmit.kuxi.activity.JoinRecordActivity;
import cn.zmit.kuxi.activity.Joinrecord;
import cn.zmit.kuxi.contants.SPConstants;
import cn.zmit.kuxi.entity.OldAnnounceEntity;

/** * @author chenxu @parameter * @return */
public class OldAnnounceHolder extends ViewHolderBase<OldAnnounceEntity> {
	private TextView title, time, luckUser, join_num, luckyNum;
	private Context context;
	private LinearLayout llRoot;

	@SuppressLint("InflateParams")
	@Override
	public View createView(LayoutInflater layoutInflater) {
		context = layoutInflater.getContext();
		View view = layoutInflater.inflate(R.layout.item_old_announced, null);
		title = (TextView) view.findViewById(R.id.tv_title_old);
		time = (TextView) view.findViewById(R.id.tv_public_date);
		luckUser = (TextView) view.findViewById(R.id.tv_luck_user);
		join_num = (TextView) view.findViewById(R.id.tv_join_num);
		luckyNum = (TextView) view.findViewById(R.id.tv_luck_num);
		llRoot = (LinearLayout) view.findViewById(R.id.ll_old);
		return view;
	}

	public interface onItemClickListener {
		void onItemClick(View view, int position);
	}

	private OnItemClickListener mOnItemClickListener;

	public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
		this.mOnItemClickListener = mOnItemClickListener;
	}

	@Override
	public void showData(int position, final OldAnnounceEntity itemData) {

		if (itemData != null) {
			int detime = itemData.getOldtime() - itemData.getNowtime();
			if (detime > 0) {
				title.setText("第" + itemData.getTitle() + "期");
				time.setText("揭晓时间：" + "等待中...");
				luckUser.setText("幸运用户：" + "敬请期待");
				join_num.setText("本期参与：" + "数据计算中...");
				luckyNum.setText("幸运号码：" + "数据计算中...");
				llRoot.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Toast.makeText(context, "即将揭晓，敬请期待！", 0).show();
					}
				});
			} else {
				title.setText("第" + itemData.getTitle() + "期");
				time.setText("揭晓时间：" + itemData.getTime());
				luckUser.setText("幸运用户：" + itemData.getName() + "  " + "("
						+ itemData.getPhone() + ")");
				join_num.setText("本期参与：" + itemData.getBuy_count());
				int num = Integer.parseInt(itemData.getLucky_num());
				int luckNum = num;
				luckyNum.setText("幸运号码：" + luckNum + "");
				final Boolean has_login = PreferenceHelper.readBoolean(context,
						SPConstants.SHARED_PREFERENCE_NAME,
						SPConstants.HAS_LOGGED, false);
				llRoot.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (has_login) {
							Intent intent = new Intent();
							Bundle bundle = new Bundle();
							bundle.putString("win_id", itemData.getUser_id());
							intent.putExtras(bundle);
							intent.setClass(context, HisCenterActivity.class);
							context.startActivity(intent);
						} else {
							Toast.makeText(context, "登陆后可查看!", 0).show();
						}
					}
				});

			}
		}

	}

}
